package com.doudou.casebook.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.doudou.casebook.R;
import com.doudou.casebook.db.DBManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 历史账单界面,点击日历表,弹出对话框,当中的GridView对应的适配器
 */
public class CalendarAdapter extends BaseAdapter {
    Context context;
    List<String>mDatas;
    public int year;
    public int selPos = -1;

    public void setYear(int year) {
        this.year = year;
        mDatas.clear();
        getMonth( year);
        notifyDataSetChanged();
    }

    public CalendarAdapter(Context context, int year) {
        this.context = context;
        this.year = year;
        mDatas=new ArrayList<>();
        getMonth( year);

    }
    //获取该年内有记录的月份
    private void getMonth(int year) {
        List<String> monthOfYearListFromAccounttb = DBManager.getMonthOfYearListFromAccounttb(year);

        mDatas.addAll(monthOfYearListFromAccounttb);
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView= LayoutInflater.from(context).inflate(R.layout.item_dialog_gv, parent,false);
        TextView tv = convertView.findViewById(R.id.item_dialogcal_gv_tv);
        String month = mDatas.get(position);
        tv.setText(year+"/"+month);
        tv.setBackgroundResource(R.color.grey_f3f3f3);
        tv.setTextColor(Color.BLACK);

        if (position==selPos) {
            tv.setBackgroundResource(R.color.colorPrimary);
            tv.setTextColor(Color.WHITE);
        }
        return convertView;
    }
}
